<?php


/**
* CakePHP Routines Plugin
* http://cake-routines-plugin.googlecode.com
*
* @package routines
* @subpackage routines.models.behaviors
* @author Ivan Chistyakov <zetruger@gmail.com>
* @version $Id: forbidable.php 171 2010-10-22 11:08:50Z zetruger@gmail.com $
* @link $HeadURL: http://cake-routines-plugin.googlecode.com/svn/trunk/models/behaviors/forbidable.php $
* @license MIT License (http://opensource.org/licenses/mit-license.php)
*/


class ForbidableBehavior extends ModelBehavior {

var $_defaults = array(
	'forbidCreate' => FALSE,
	'forbidSave' => FALSE,
	'forbidDelete' => FALSE
);

var $settings = array();

function setup(&$Model, $settings = array()) {
	$this->settings[$Model->alias] = $settings;
}

function _getSettings(&$Model, $settings = array()) {
	$settings = Set::merge($this->settings[$Model->alias], $settings);
	if (isset($settings['noUseModelVars']) && $settings['noUseModelVars']) {
		return Set::merge($this->_defaults, $settings);
	} else {
		return Set::merge($this->_defaults, array_intersect_key(get_object_vars($Model), $this->_defaults), $settings);
	}
}

function _creating(&$Model) {
	return empty($Model->id) && (!isset($Model->data[$Model->alias][$Model->primaryKey]) || empty($Model->data[$Model->alias][$Model->primaryKey]));
}

function beforeValidate(&$Model) {
	extract($this->_getSettings($Model), EXTR_SKIP);
	$creating = $this->_creating($Model);
	if (($creating && $forbidCreate) || (!$creating && $forbidSave)) {
		return FALSE;
	} else {
		return TRUE;
	}
}

function beforeSave(&$Model) {
	extract($this->_getSettings($Model), EXTR_SKIP);
	$creating = $this->_creating($Model);
	if (($creating && $forbidCreate) || (!$creating && $forbidSave)) {
		return FALSE;
	} else {
		return TRUE;
	}
}

function beforeDelete(&$Model) {
	extract($this->_getSettings($Model), EXTR_SKIP);
	if ($forbidDelete) {
		return FALSE;
	} else {
		return TRUE;
	}
}

}


?>